#coding=utf-8
from uliweb.orm import *
from uliweb.utils.common import get_var
from uliweb.i18n import ugettext_lazy as _

def get_modified_user():
    from uliweb import request
    
    return request.user.id

class SelfDevPoll_Modules(Model):
    __verbose_name__ = u'优秀模块投票'
    
    poll = Reference('poll', verbose_name='投票', required=True)
    name = Field(str, max_length=255, verbose_name='模块名称', required=True)
    order = Field(int, verbose_name='序号')
    link = Field(str, max_length=255, verbose_name='说明链接')
    kfzx = Reference('kfzx', verbose_name='开发中心', required=True)
    
    def __unicode__(self):
        return self.name
    
    def get_url(self):
        return u'<a href="/ticket/q/%d" title="%s">#%d</a>' % (self.id, self.name, self.id)
    
    class AddForm:
        fields = [
            'name', 'order', 'link', 'kfzx'
        ]
        
    class EditForm:
        fields = [
            'name', 'order', 'link', 'kfzx'
        ]

    class Table:
        fields = [
            {'name':'name', 'width':300},
            {'name':'link', 'width':300},
            {'name':'kfzx', 'width':100}, 
            {'name':'action', 'verbose_name':'操作', 'width':60}
        ]
    
class SelfDevPoll_User(Model):
    __verbose_name__ = u'优秀模块投票记录'
    
    username = Field(str, max_length=40, verbose_name='姓名', required=True)
    email = Field(str, max_length=80, verbose_name='邮箱', required=True, unique=True)
    depart = Reference('kfzx', verbose_name='所属单位', required=True)
    mobile = Field(str, max_length=20, verbose_name='联系电话')
    selections = ManyToMany(SelfDevPoll_Modules, through='selfdev_selection')
    enabled = Field(bool, verbose_name='是否有效')
    active_code = Field(str, max_length=32, verbose_name='激活码', index=True)
    expiry_time = Field(datetime.datetime, verbose_name='有效期')
    active_time = Field(datetime.datetime, verbose_name='激活时间')
    create_time = Field(datetime.datetime, verbose_name='创建时间')
    ip = Field(str, max_length=15, verbose_name='IP地址')
    
    class AddForm:
        fields = [
            'username', 'email', 'depart', 'mobile',
        ]
        layout = [
            ('username', 'email'), 
            ('depart', 'mobile',)
        ]
        
    class Table:
        fields = [
            {'name':'enabled', 'width':60},
            {'name':'username', 'width':100},
            {'name':'email', 'width':120},
            {'name':'depart', 'width':120},
        ]
        
class SelfDev_Selection(Model):
    module = Reference(SelfDevPoll_Modules)
    user = Reference(SelfDevPoll_User)
    award = Field(CHAR, max_length=1, verbose_name='奖项', choices=get_var('SELFDEVPOLL/awards'))
    kfzx = Reference('kfzx', verbose_name='开发中心')